<?php

// ---------------------------------------------------------------------------------------
//
// rFactor Server Query PHP Class by Rene - rene@wiesental-racing.de
//                                          www.wiesental-racing.de
//
// Das Script kann gerne jederzeit ge�ndert oder erweitert werden. Vorrausetzung
// daf�r ist, dass dieser komplette Kommentarblock und die beiliegende ReadMe erhalten bleiben.
// Es w�re sch�n, wenn ihr eigene Erweiterungen/Verbesserungen an mich sendet, damit ich
// sie zentral aufnehmen kann und alle etwas davon haben. (9.6.2006)
//
// Der Code wurde unter PHP 4.2.2 und 4.3.11 getestet, m�sste aber auch unter PHP 5 lauff�hig sein.
// F�r Fehler irgendeiner Art die durch die Benutzung des Code veruracht werden �bernehme ich
// selbstverst�ndlich keinerlei Haftung.
//
// ---------------------------------------------------------------------------------------


include_once('includes/rfquery/rfpluginquery.php');

define("RFPQDI_SESSION_RACE", 7);
define("RFPQDI_GPHASE_GREEN", 5);

define("RFQDI_FS_FINISHED", 1);
define("RFQDI_FS_DNQ",      2);
define("RFQDI_FS_DQ",       3);


if (!class_exists("rFDriverInfo"))
{



class rFDriver
{
	var $driver_name;
	var $is_player;
        var $ai;
	var $time_behind_leader;
	var $laps_behind_leader;
	var $total_laps;
	var $finished_status;
	var $laptime_best;
	var $num_pitstops;
	var $num_penalties;
	var $place;
	var $vehicle_name;
        var $leader_gap;
}



function _compareDriversPlace($drv1, $drv2)
{
	return ($drv1->place < $drv2->place) ? -1 : 1;
}
	

function StatusToText($finish_status) {
    global $lang;
    $stat = '';

    switch ($finish_status) {
        case RFQDI_FS_FINISHED:
            $stat = $lang['server_finished'];
            break;

        case RFQDI_FS_DNQ:
            $stat = $lang['server_dnq'];
            break;

        case RFQDI_FS_DQ:
            $stat = $lang['server_dq'];
            break;
    }

    return $stat;
}


class rFDriverInfo extends rFPluginQuery
{
	var $cmd_result;
	var $drivers = array();
	
	var $cur_session;
	var $game_phase;


	function rFDriverInfo($host, $port, $read_blocked = true)
	{
		$this->rFPluginQuery($host, $port, $read_blocked);
		$this->cmd_result = false;
	}

	
	// Call this function to connect to the server and retrieve information from it.
	// Must be called before you query the details using the _getXXX/_isXXX methods.
	function _queryDriverInfo($timeout = 4)
	{
		$this->drivers = array();
		$response = "";
		$response = $this->_sendCommand("QD", $timeout);
		//echo "DataSize: " . strlen($response) . "<br><br>";
		if (strlen($response) >= 4)
		{
			//echo "Response len: " . strlen($response) . "<br><br>";

			$main_block_size = $this->_ShortFromBuffer($response, 0x00);
			$veh_block_size = $this->_ShortFromBuffer($response, 0x02);
			
			$num_vehicles =  $this->_LongFromBuffer($response, 0x04);
			$this->cur_session = $this->_LongFromBuffer($response, 0x08);
			$this->game_phase = $this->_ByteFromBuffer($response, 0x0C);

			$buffer_offset = $main_block_size;

			for ($i = 0; $i < $num_vehicles; $i++)
			{			
				$drv = $this->_getDriver($response, $buffer_offset);
				$buffer_offset += $veh_block_size;
				
				$this->drivers[] = $drv;

			}

			if (0 < $num_vehicles)
			{
				usort($this->drivers, "_compareDriversPlace");	
			}

			$this->cmd_result = true;
		}
		else
		{
			$this->cmd_result = false;
		}
		
		return $this->cmd_result;
	}
	
	
	function _getDriver($buffer, $buffer_offset)
	{
                global $lang;
		$driver = new rFDriver;
		
		$driver->driver_name     = htmlentities($this->_StringFromBuffer($buffer, 32, $buffer_offset));
		$driver->is_player       = $this->_ShortFromBuffer ($buffer, $buffer_offset + 32) == 1;
                $driver->ai              = $driver->is_player ? "" : $lang['server_ai'];
		$driver->time_behind_leader = $this->_LongFromBuffer ($buffer, $buffer_offset + 34) / 10000;
		$driver->laps_behind_leader = $this->_LongFromBuffer ($buffer, $buffer_offset + 38);
		
		$driver->total_laps      = $this->_ShortFromBuffer ($buffer, $buffer_offset + 42);
		$driver->finished_status = StatusToText($this->_ByteFromBuffer  ($buffer, $buffer_offset + 44));
		$driver->laptime_best    = $this->_LongFromBuffer  ($buffer, $buffer_offset + 45) / 10000;
                $driver->laptime_best    = 0 < $driver->laptime_best ? SecondsToString($driver->laptime_best) : "";
		$driver->num_pitstops    = $this->_ShortFromBuffer ($buffer, $buffer_offset + 49);
		$driver->num_penalties   = $this->_ShortFromBuffer ($buffer, $buffer_offset + 51);
		
		$driver->place           = $this->_ByteFromBuffer  ($buffer, $buffer_offset + 53);
		$driver->vehicle_name    = htmlentities($this->_StringFromBuffer($buffer, 32, $buffer_offset + 54));

	        $distance = '';

                if (0 < $driver->laps_behind_leader) {
                    $laptxt = (1 < $driver->laps_behind_leader) ? $lang['server_laps'] : $lang['server_lap'];
                    $distance = '+' . $driver->laps_behind_leader . ' '. $laptxt;
                }
                else if ((0 < $driver->time_behind_leader) && (RFPQDI_GPHASE_GREEN <= $this->game_phase)) {
                        $distance = '+' . SecondsToString($driver->time_behind_leader);
                    }

                if (0 == $driver->finished_status) {
                    $driver->leader_gap = $distance;
                }
                else {
                    $driver->leader_gap = StatusToText($lang, $driver->finished_status);
                }
		
		if (0 < $driver->num_pitstops) $driver->num_pitstops--;

		return $driver;	
	}
	
	

}

} // if (!class_exists

?>